<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
	String isSP = request.getParameter("isSP");
	String status = request.getParameter("status") == null ? "" : request.getParameter("status");
	if("".equals(status)){
		status = "0";
	}
	String gd_id = request.getParameter("gd_id") == null ? "" : request.getParameter("gd_id");
	String gd_content_id = request.getParameter("gd_content_id") == null ? "" : request.getParameter("gd_content_id");
	String gd_code = request.getParameter("gd_code") == null ? "" : request.getParameter("gd_code");
	String gd_name = request.getParameter("gd_name") == null ? "" : request.getParameter("gd_name");
	String xm_code = request.getParameter("xm_code") == null ? "" : request.getParameter("xm_code");
	String xm_name = request.getParameter("xm_name") == null ? "" : request.getParameter("xm_name");
%>
<html>
	<head>
		<title>打印预览</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
			.datagrid-cell {
			  font-size: 18px;
			}
		</style>
	<script type="text/javascript">
		$(function(){
			var url = '<%=contextPath %>/springMVC/gd/listGDMaterialBalance?type=1&gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>';
			$.ajax({
				type:'POST',
				url:'<%=contextPath %>/springMVC/gd/hasBalance?type=1&gd_id=<%=gd_id%>',
				async:false,
				success:function(rtjson){
					if(rtjson != '0'){
						url = '<%=contextPath %>/springMVC/gd/listGDMaterialBalanceByGdId?type=1&gd_id=<%=gd_id%>&gd_content_id=<%=gd_content_id%>';
					}
				}
			});
			
			$('#table').datagrid({
				title:'绿色背景表示该材料使用量低于管控最小值',
				url:url,
				rownumbers:true,
				singleSelect:true,
				fitColumns:true,
				showFooter:true,
				frozenColumns:[[
				    {field:'id',hidden:true},
				    {title:'产品id',field:'material_id',hidden:true}
				]],
				columns:[[
					{title:'产品名称',field:'name',width:150,align:'center',rowspan:2},
					{title:'规格/型号',field:'format_type',width:150,align:'center',rowspan:2},
					{title:'单位',field:'unit_name',width:50,align:'center',rowspan:2},
					{title:'预算数量',field:'assume_num',width:100,align:'center',rowspan:2},
					{title:'管控精度(%)',field:'control',width:100,align:'center',rowspan:2},
					{title:'最小管控值',field:'min_num',width:100,align:'center',rowspan:2,formatter:function(value,row,index){
						if(row.assume_num == undefined){
							return;
						}
						var assume_num = parseFloat(row.assume_num);
					    if(assume_num == 0){
					    	return 0;
					    }
			    	    var control = parseFloat(row.control)/100;
			    	    var min_num = Math.round(assume_num*(1-control)*Math.pow(10,2))/Math.pow(10,2);
			    	    return  min_num;
					}},
					{title:'已用合计',field:'already_use',width:100,align:'center',rowspan:2},
					{title:'订单',width:300,align:'center',colspan:2},
					{title:'工余',width:200,align:'center',colspan:2},
					{title:'现场新用数',field:'xc_use',width:150,align:'center',rowspan:2,editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'现场利旧',width:200,align:'center',colspan:2},
					{title:'施工队',width:200,align:'center',colspan:2},
					{title:'订单工余料',field:'order_left',width:150,align:'center',rowspan:2,formatter:function(value,rowData,index){
							rowData.order_left = parseFloat(rowData.purchase_num)-parseFloat(rowData.purchase_use_num);
							if(isNaN(rowData.order_left)){
								return "";
							}
							return rowData.order_left;
					}},
					{title:'拆旧回收',width:200,align:'center',colspan:2},
					{title:'订单领用总额',field:'purchase_use_cost',hidden:true},
					{title:'材料经济分析',width:600,align:'center',colspan:4}
				],[
					{title:'采购数',field:'purchase_num',width:100,align:'center'},
					{title:'领用数',field:'purchase_use_num',width:100,align:'center'},
					{title:'领用数',field:'my_use',width:100,align:'center'},
					{title:'总价',field:'my_cost',width:80,align:'center'},
					{title:'利旧数',field:'use_old_num',width:100,align:'center'},
					{title:'总价',field:'use_old_cost',width:80,align:'center'},
					{title:'工余料',field:'worker_left_num',width:100,align:'center',formatter:function(value,rowData,index){
							rowData.worker_left_num = parseFloat(rowData.xc_use)-parseFloat(rowData.my_use)-parseFloat(rowData.purchase_use_num);
							if(isNaN(rowData.worker_left_num)){
								return "";
							}
							return rowData.worker_left_num;
					}},
					{title:'单价',field:'worker_left_cost',width:80,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'回收数',field:'recovery_num',width:100,align:'center'},
					{title:'总价',field:'recovery_cost',width:80,align:'center'},
					{title:'材料采购费',field:'purchase_cost',width:150,align:'center'},
					{title:'施工队材料款',field:'worker_money',width:200,align:'center',formatter:function(value,rowData,index){
						if(rowData.name == '合计:'){
							return value;
						}
						rowData.worker_money = Math.round(parseFloat(rowData.worker_left_num)*parseFloat(rowData.worker_left_cost)*Math.pow(10,2))/Math.pow(10,2);
						return rowData.worker_money;
					}},
					{title:'实际材料成本',field:'actual_money',width:200,align:'center',formatter:function(value,rowData,index){
							if(rowData.name == '合计:'){
								return value;
							}
							rowData.actual_money = parseFloat(rowData.my_cost)+parseFloat(rowData.purchase_use_cost);
							return rowData.actual_money;
					}},
					{title:'材料管控指标',field:'target_money',width:200,align:'center',formatter:function(value,rowData,index){
							if(rowData.name == '合计:'){
								return value;
							}
							rowData.target_money = parseFloat(rowData.actual_money)-parseFloat(rowData.purchase_cost)+parseFloat(rowData.recovery_cost);
							return rowData.target_money;
					}}
				   ]],
				   rowStyler : function(index,row){
					    var assume_num = parseFloat(row.assume_num);
					    if(assume_num == 0){
					    	return 'background-color:#fff;color:black;';
					    }
			    	    var control = parseFloat(row.control)/100;
			    	    var min_num = Math.round(assume_num*(1-control)*Math.pow(10,2))/Math.pow(10,2);
			    	    var max_num = Math.round(assume_num*(1+control)*Math.pow(10,2))/Math.pow(10,2);
			    	    
						if(parseFloat(row.already_use) < parseFloat(min_num))
						{	
							return 'background-color:#0aa344;color:black;';
						}
						if(parseFloat(max_num) < parseFloat(row.already_use))
						{	
							return 'background-color:#ffee00;color:red;';
						}
						return 'background-color:#fff;color:black;';
					},
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onLoadSuccess:function(data){
					showFooter();
				},
				onAfterEdit:function(){
					showFooter();
				}
				
			});
		});
		function showFooter(){
			var jsondata=$("#table").datagrid("getData");
			var purchase_cost_total = 0;
			var worker_money_total = 0;
			var actual_money_total = 0;
			var target_money_total = 0;
			for(var i=0;i<jsondata.total;i++){
				purchase_cost_total += jsondata.rows[i].purchase_cost;
				worker_money_total += jsondata.rows[i].worker_money;
				actual_money_total += jsondata.rows[i].actual_money;
				target_money_total += jsondata.rows[i].target_money;
			}
			$('#table').datagrid('reloadFooter',[
			                              	{name:'合计:',
			                              	 purchase_cost: purchase_cost_total, 
			                              	 worker_money: worker_money_total,
			                              	 actual_money: actual_money_total,
			                              	 target_money: target_money_total
			                              	 }
			                              ]);
		}
		function startPrint(obj){
			$(obj).hide();
			window.print();
		}
	</script>
	</head>
	<body>
		<div >
		<form id="ff">
			<div style="text-align: center;">
					<p style="font-weight: bolder;">乙供材料平衡</p>
			</div>
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr style="display: none;">
					<td align="right" class="td1" nowrap>项目编号：</td>
					<td class="td2" nowrap>
						<span  id="xm_code" name="xm_code"  ><%=xm_code%></span>
					</td>
					<td align="right" class="td1" nowrap>项目名称：</td>
					<td class="td2" nowrap>
						<span  id="xm_name" name="xm_name" ><%=xm_name%></span>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>工单编号：</td>
					<td class="td2" nowrap>
						<span  id="gd_code" name="gd_code" ><%=gd_code%></span>
					</td>
					<td align="right" class="td1" nowrap>工单名称：</td>
					<td class="td2" nowrap>
						<span  id="gd_name" name="gd_name" ><%=gd_name%></span>
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>
					</td>
					<td class="td2" nowrap colspan='3'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print'" onclick="startPrint(this);">确认打印</a>
						</div>
					</td>
				</tr>
			</table>
			</form>
			<div id="table" ></div>
		</div>
	</body>
</html>